import { setToken, getToken, removeToken } from '@/utils/auth'
import { login } from '@/api/user'
import router from '@/router'
export default {
  namespaced: true,
  state: {
    token: getToken()
  },
  mutations: {
    setTokenFn(state, payload) {
      setToken(payload)
      state.token = payload
    },
    removeToken(state) {
      removeToken()
      state.token = null
    }
  },
  actions: {
    async loginFn(ctx, payload) {
      const { data } = await login(payload)
      ctx.commit('setTokenFn', data)
      router.push('/dashboard')
    }
  }
}
